//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for RegistrationOptions.
	/// </summary>
	public class RegistrationOptions : BaseMembershipPage
	{

		#region Members

		protected TextBox txtUserNameRegEx;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected TextBox txtUsernameMinLength;
		protected RequiredFieldValidator UsernameMinLengthValidator;
		protected TextBox txtUsernameMaxLength;
		protected RequiredFieldValidator UsernameMaxLengthValidator;
		protected TextBox txtPasswordRegEx;
		
		protected YesNoRadioButtonList optAllowLogin;
		protected YesNoRadioButtonList optAllowNewUserRegistration;
		protected YesNoRadioButtonList optShowContactCheckboxes;
		protected ModerationLevelDropDownList ddlNewUserModerationLevel;
		protected AccountActivationRadioButtonList optAccountActivation;
		protected PasswordRecoveryRadioButtonList optPasswordRecovery;
		protected YesNoRadioButtonList optEnableBannedUsersToLogin;

		protected StatusMessage formStatus;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl Resourcecontrol1;
		protected MPContent DescriptionRegion;
		protected HelpIcon Helpicon1;
		protected ResourceLabel Resourcelabel7;
		protected HelpIcon Helpicon2;
		protected ResourceLabel Resourcelabel1;
		protected HelpIcon Helpicon3;
		protected ResourceLabel Resourcelabel9;
		protected HelpIcon Helpicon4;
		protected ResourceLabel Resourcelabel11;
		protected HelpIcon Helpicon5;
		protected ResourceLabel ResourceLabel2;
		protected HelpIcon Helpicon6;
		protected ResourceLabel Resourcelabel13;
		protected HelpIcon Helpicon7;
		protected ResourceLabel Resourcelabel14;
		protected HelpIcon Helpicon8;
		protected ResourceLabel Resourcelabel17;
		protected HelpIcon Helpicon9;
		protected ResourceLabel Resourcelabel19;
		protected HelpIcon Helpicon10;
		protected ResourceLabel ResourceLabel3;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton btnSave;

		#endregion


		override protected void OnInit(EventArgs e)
		{
			btnSave.Click += new EventHandler(Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
				this.DataBind();
		}

		public override void DataBind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
    
			formStatus.Visible = false;
			optAllowLogin.SelectedValue = settings.AllowLogin;
			optAllowNewUserRegistration.SelectedValue = settings.AllowNewUserRegistration;
			optShowContactCheckboxes.SelectedValue = settings.ShowContactCheckboxes;
			ddlNewUserModerationLevel.SelectedValue = settings.NewUserModerationLevel;
			txtUsernameMinLength.Text = settings.UsernameMinLength.ToString();
			txtUserNameRegEx.Text = settings.UsernameRegex;
			txtPasswordRegEx.Text = settings.PasswordRegex;
			txtUsernameMaxLength.Text = settings.UsernameMaxLength.ToString();
			optAccountActivation.SelectedValue = settings.AccountActivation;
			optPasswordRecovery.SelectedValue = settings.PasswordRecovery;
			optEnableBannedUsersToLogin.SelectedValue = settings.EnableBannedUsersToLogin;

		}
	
		private void Save_Click(Object sender, EventArgs e) 
		{
			if (!Page.IsValid)
				return;
			
			SaveSettings();

			formStatus.Visible = true;
			formStatus.Success = true;
			formStatus.Text = string.Format(ResourceManager.GetString("CP_Membership_Cache_Warning"), CSContext.Current.SiteSettings.SiteSettingsCacheWindowInMinutes).Replace("'", @"\'");
		}

		protected virtual void SaveSettings() 
		{
			SiteSettings settings = CSContext.Current.SiteSettings; 

			try { settings.UsernameMaxLength = int.Parse(txtUsernameMaxLength.Text); }
			catch { ((RequiredFieldValidator)FindControl( "UsernameMaxLengthValidator" )).IsValid = false; }
			
			try { settings.UsernameMinLength = int.Parse(txtUsernameMinLength.Text); }
			catch { ((RequiredFieldValidator)FindControl( "UsernameMinLengthValidator" )).IsValid = false; }

			settings.UsernameRegex = txtUserNameRegEx.Text;
			settings.PasswordRegex = txtPasswordRegEx.Text;
			settings.AllowLogin = optAllowLogin.SelectedValue;
			settings.AllowNewUserRegistration = optAllowNewUserRegistration.SelectedValue;
			settings.ShowContactCheckboxes = optShowContactCheckboxes.SelectedValue;
			settings.NewUserModerationLevel = ddlNewUserModerationLevel.SelectedValue;
			settings.AccountActivation = optAccountActivation.SelectedValue;
			settings.PasswordRecovery = optPasswordRecovery.SelectedValue;
			settings.EnableBannedUsersToLogin = optEnableBannedUsersToLogin.SelectedValue;

			SiteSettingsManager.Save(settings);
		}
	}
}
